import { SessionError } from "koishi"

export const name = "command-filter"

export function apply(ctx) {
  ctx.before("command/execute", async ({ session, source }) => {
    ctx.logger.debug(source)
    if (source?.replace(/(data:[0-9a-z-]+\/[0-9a-z-]+;base64,|base64:\/\/)[0-9A-Za-z/+]+/, "")
        .match(/((?<!\d)|(?<=1\W*9))\W*8\W*9\D*?(6\D*?|0?\W*6\D*?0\W*)4(?!\W*[1-9])/)) {
      if (session.isDirect || !session.onebot) return session.text("lnnbot.filter.input-bad-number")
      session.onebot?._request("set_msg_emoji_like", { message_id: session.messageId, emoji_id: 10 }).catch(() => {})
      throw new SessionError("")
    }
  })

  ctx.i18n.define("zh-CN", "lnnbot.filter.input-bad-number", "输入疑似包含敏感数字…")
}
